package com.weizhizs.assistant.code.template.data;

import com.weizhizs.assistant.code.config.ParamConfig;
import com.weizhizs.assistant.code.helper.BeanHelper;
import com.weizhizs.assistant.code.helper.TableHelper;
import com.weizhizs.assistant.code.template.IDataLoador;
import com.weizhizs.assistant.code.vo.TableVO;
import com.weizhizs.assistant.utils.DateUtils;

import java.util.HashMap;
import java.util.Map;

public class JavaDaoData implements IDataLoador {

    public static String getClassName(String tableName) {
        return TableHelper.tableNameToClassName(tableName) + "Dao";
    }

    public static String getFullClassName(String classPath, String tableName) {
        return classPath + "." + getClassName(tableName);
    }

    @Override
    public String getTemplateName(TableVO tableVO) {
        return ParamConfig.getInstance().getTemplatePath() + "/dao.java.vm";
    }

    @Override
    public String getFileName(String tableName) {
        return getClassName(tableName) + ".java";
    }

    @Override
    public String getFileOutPath() {
        return ParamConfig.getInstance().getDaoOutPath();
    }

    @Override
    public Map<String, Object> getDataMap(TableVO tableVO) {
        String tableName = tableVO.getTableName();

        Map<String, Object> map = new HashMap<>();
        map.put("tableName", tableName);
        map.put("tableComment", tableVO.readTableComment());
        map.put("tableCommentFull", tableVO.getTableComment());
        map.put("authorName", ParamConfig.getInstance().getAuthorName());
        map.put("version", ParamConfig.getInstance().getVersion());
        map.put("currentDate", DateUtils.nowDateString());

        map.put("entityOutPath", getFileOutPath());
        map.put("entityClassName", getClassName(tableName));

        map.put("dtoClassName", JavaDtoData.getClassName(tableName));
        map.put("dtoFullClassName", JavaDtoData.getFullClassName(ParamConfig.getInstance().getDtoOutPath(), tableName));

        String daoBaseFullClassName = ParamConfig.getInstance().getDaoBaseClass();
        String daoBaseClassName = BeanHelper.getBeanClassName(ParamConfig.getInstance().getDaoBaseClass());
        map.put("daoBaseFullClassName", daoBaseFullClassName);
        map.put("daoBaseClassName", daoBaseClassName);
        if (TableVO.TABLE_TYPE_DEL == tableVO.getTableType()) {
            daoBaseFullClassName = daoBaseFullClassName.replace(daoBaseClassName, "BaseAllDao");
            map.put("doBaseFullClassName", daoBaseFullClassName);
            map.put("doBaseClassName", "BaseAllDao");
        } else {
            daoBaseFullClassName = daoBaseFullClassName.replace(daoBaseClassName, "BaseEditDao");
            map.put("doBaseFullClassName", daoBaseFullClassName);
            map.put("doBaseClassName", "BaseEditDao");
        }

        return map;
    }

}
